package com.ruoyi.tws.service.impl;

import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.domain.TwsUseRecord;
import com.ruoyi.tws.service.IOrderService;
import com.ruoyi.tws.service.ITwsUseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.web.controller.BaseServiceImpl;
import com.ruoyi.tws.mapper.OrderSecureMapper;
import com.ruoyi.tws.domain.OrderSecure;
import com.ruoyi.tws.service.IOrderSecureService;

import java.util.Date;

/**
 * 解除运单申请Service业务层处理
 *
 * @author wjx
 * @date 2023-05-04
 */
@Service
public class OrderSecureServiceImpl extends BaseServiceImpl<OrderSecureMapper, OrderSecure> implements IOrderSecureService {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private ITwsUseRecordService useRecordService;

    @Override
    public boolean updateSecure(Order order) {
        OrderSecure orderSecure = new OrderSecure();
        orderSecure.setId(order.getSecureId());
        orderSecure.setStatus(1);
        updateById(orderSecure);

        //将订单再次转为待发车
        order.setStatus("1");
        orderService.updateById(order);

        //操作日志
        TwsUseRecord useRecord = new TwsUseRecord();
        useRecord.setOrderId(order.getId());
        useRecord.setOperateContent("通过解除运单申请并转单至:"+order.getDriverName());
        useRecord.setOperateTime(new Date());
        useRecord.setOperator(SecurityUtils.getUsername());
        useRecordService.save(useRecord);

        return true;
    }

    @Override
    public boolean rejectSecure(OrderSecure orderSecure) {
        Order order = new Order();
        order.setId(orderSecure.getOrderId());
        order.setStatus("1");
        orderService.updateById(order);

        TwsUseRecord useRecord = new TwsUseRecord();
        useRecord.setOrderId(orderSecure.getOrderId());
        useRecord.setOperateContent("驳回解除运单申请");
        useRecord.setOperateTime(new Date());
        useRecord.setOperator(SecurityUtils.getUsername());
        useRecordService.save(useRecord);

        orderSecure.setStatus(2);
        updateById(orderSecure);
        return true;
    }
}
